package com.tengju.bff.interfaces.manage.crm.motivation.facade;

import com.beust.jcommander.internal.Lists;
import com.google.common.collect.Maps;
import com.tengju.bff.interfaces.manage.crm.motivation.form.GetActivityPerformanceObjectiveListForm;
import com.tengju.bff.interfaces.manage.crm.motivation.form.GetMotivationActivityConfigForm;
import com.tengju.bff.interfaces.manage.crm.motivation.vo.MotivationActivityConfigDetailVO;
import com.tengju.bff.interfaces.shared.ColumnVO;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.PageResultVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.motivation.domain.model.performance.MotivationActivityConfig;
import com.tengju.motivation.domain.model.performance.MotivationActivityConfigObjectTypeEnum;
import com.tengju.motivation.domain.model.performance.*;
import com.tengju.motivation.domain.shared.DateUtil;
import com.tengju.motivation.domain.shared.PerformanceObjectiveType;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: 李大仙
 * @date: 2021/7/22 3:03 下午
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MotivationActivityConfigFacade {

//    private final MotivationActivityConfigRepository motivationActivityConfigRepository;
    private final PerformanceObjectiveRepository performanceObjectiveRepository;

    public TableVO getByPage(GetMotivationActivityConfigForm form) {
        Integer pageSize = form.getPageSize();
        Integer pageIndex = form.getPageIndex();
        int offset = form.getOffset();

        TableVO table = new TableVO();
        List<ColumnVO> header = List.of(
                ColumnVO.of("title", "活动名称", false),
                ColumnVO.of("time", "活动时间", false),
                ColumnVO.of("objectType", "类型", false)
        );
        table.setHeader(header);

//        int total = motivationActivityConfigRepository.getTotal(form.getTitle());

        List<MotivationActivityConfig> motivationActivityConfigList = new ArrayList<>();

//        if (offset <= total) {
//            motivationActivityConfigList = motivationActivityConfigRepository.queryByTitlePage(
//                    form.getTitle(),
//                    form.getOffset(),
//                    form.getPageSize());
//        }

        List<Map<String, Object>> records = new ArrayList<>();

        for (MotivationActivityConfig motivationActivityConfig : motivationActivityConfigList) {
            Map<String, Object> row = Maps.newHashMap();
            row.put("title", motivationActivityConfig.getTitle());
            row.put("time", getTime(motivationActivityConfig.getStartTime(), motivationActivityConfig.getEndTime()));
            row.put("objectType", motivationActivityConfig.getObjectType().getDesc());
            row.put("activityId", motivationActivityConfig.getActivityId().getValue());
            row.put("remark", motivationActivityConfig.getRemark());
            row.put("startTime", DateUtil.format(motivationActivityConfig.getStartTime()));
            row.put("endTime", DateUtil.format(motivationActivityConfig.getEndTime()));
            records.add(row);
        }
        table.setRecords(records);
        table.setPage(PageResultVO.of(pageIndex, pageSize, 0));
        return table;
    }

    private String getTime(LocalDateTime startTime, LocalDateTime endTime) {
        String result = "%s 至 %s";
        return String.format(result,
                DateUtil.formatLocalDateTime(DateUtil.DATE_TIME_MINUTE_FORMAT, startTime),
                DateUtil.formatLocalDateTime(DateUtil.DATE_TIME_MINUTE_FORMAT, endTime));
    }

    public MotivationActivityConfigDetailVO detail(Long activityId) {
        MotivationActivityConfigDetailVO result = new MotivationActivityConfigDetailVO();
//        MotivationActivityConfig activityConfig = motivationActivityConfigRepository.getByActivityId(new MotivationActivityConfigId(activityId));
//        if (activityConfig != null) {
//            result.setTitle(activityConfig.getTitle());
//            result.setObjectType(activityConfig.getObjectType().getDesc());
//            result.setTime(getTime(activityConfig.getStartTime(), activityConfig.getEndTime()));
//            result.setRemark(activityConfig.getRemark());
//            TableVO tableVO = new TableVO();
//            tableVO.setHeader(List.of(
//                    ColumnVO.of("name", "目标名称", false),
//                    ColumnVO.of("value", "目标值", false)
//            ));
//            List<PerformanceObjectiveTotal> performanceObjectiveTotalList = performanceObjectiveRepository.getPerformanceObjectiveTotalByRelationId(activityConfig.getActivityId().getValue());
//            List<Map<String, Object>> records = new ArrayList<>();
//            for (PerformanceObjectiveTotal performanceObjectiveTotal : performanceObjectiveTotalList) {
//                Map<String, Object> record = Maps.newHashMap();
//                record.put("name", PerformanceObjectiveType.getByCode(performanceObjectiveTotal.getKey()).getTargetDesc());
//                record.put("value", performanceObjectiveTotal.getValue());
//                records.add(record);
//            }
//            tableVO.setRecords(records);
//            result.setTarget(tableVO);
//        }
        return result;
    }

    public TableVO exportActivityPerformanceObjective(GetActivityPerformanceObjectiveListForm form) {
        Integer pageSize = form.getPageSize();
        Integer pageIndex = form.getPageIndex();
        int offset = form.getOffset();

        TableVO table = new TableVO();
        List<ColumnVO> header = List.of(
                ColumnVO.of("secIdCode", "SEC用户ID", false),
                ColumnVO.of("name", "目标名称", false),
                ColumnVO.of("value", "目标值", false)
        );
        table.setHeader(header);

//        MotivationActivityConfig activityConfig = motivationActivityConfigRepository.getByActivityId(new MotivationActivityConfigId(form.getActivityId()));
//        if (activityConfig == null) {
//            return table;
//        }
//        Long startTime = DateUtil.getSecondLong(activityConfig.getStartTime());
//        Long endTime = DateUtil.getSecondLong(activityConfig.getEndTime());

//        int total = performanceObjectiveRepository.getListTotal(startTime, endTime, form.getChiefId(), PerformanceObjectiveTypeEnum.GEN_SEC.getCode(), form.getSecIdCode());

        List<PerformanceObjective> performanceObjectiveList = Lists.newArrayList();
//        if (offset <= total) {
//            List<PerformanceObjectiveObjectId> objectIdList = performanceObjectiveRepository.getObjectIdList(startTime, endTime, form.getChiefId(), PerformanceObjectiveTypeEnum.GEN_SEC.getCode(), form.getSecIdCode(), offset, pageSize);
//            performanceObjectiveList = performanceObjectiveRepository.getByObjectIdAndType(objectIdList, PerformanceObjectiveTypeEnum.GEN_SEC, startTime, endTime);
//        }

        List<Map<String, Object>> records = new ArrayList<>();
        for (PerformanceObjective performanceObjective : performanceObjectiveList) {
            Map<String, Object> row = Maps.newHashMap();
            row.put("secIdCode", performanceObjective.getObjectId().getValue());
            row.put("name", performanceObjective.getName());
            row.put("value", performanceObjective.getValue().intValue());
            records.add(row);
        }
        table.setRecords(records);
        table.setPage(PageResultVO.of(pageIndex, pageSize, 0));
        return table;
    }

    public List<KVVO<String, Integer>> getObjectTypeList() {
        return Arrays.stream(MotivationActivityConfigObjectTypeEnum.values())
                .map(objectType -> KVVO.of(objectType.getDesc(), objectType.getCode()))
                .collect(Collectors.toList());
    }

    public List<String> getImportDesc() {
        List<String> result = Lists.newArrayList();
        result.add("说明：只支持EXCEL文件上传，相同日期目标导入将被覆盖更新");
        result.add("文件格式：SEC ID、目标名称、目标值");
        result.add("目标名称可选：" + StringUtils.join(PerformanceObjectiveType.getTargetDescList(), "、"));
        return result;
    }
}
